<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>线程池配置指南：存活时间与队列长度的艺术</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            bottom: -50%;
            left: -50%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 4s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 0.5; }
            50% { transform: scale(1.1); opacity: 0.8; }
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            position: relative;
            display: inline-block;
            font-family: 'Noto Serif SC', serif;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-block::before {
            content: 'Java';
            position: absolute;
            top: 10px;
            right: 15px;
            font-size: 12px;
            color: #666;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        .parameter-card {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 2rem;
            border-radius: 16px;
            position: relative;
            overflow: hidden;
        }
        
        .parameter-card::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.2) 0%, transparent 70%);
            animation: rotate 10s linear infinite;
        }
        
        @keyframes rotate {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }
        
        .list-item {
            position: relative;
            padding-left: 2rem;
        }
        
        .list-item::before {
            content: '';
            position: absolute;
            left: 0;
            top: 0.5rem;
            width: 8px;
            height: 8px;
            background: #667eea;
            border-radius: 50%;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6 relative">
        <div class="max-w-6xl mx-auto text-center relative z-10">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                线程池配置的艺术
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                深入理解存活时间与队列长度的平衡之道
            </p>
            <div class="flex justify-center space-x-8 text-lg">
                <div class="flex items-center">
                    <i class="fas fa-clock mr-2"></i>
                    <span>Keep-Alive Time</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-layer-group mr-2"></i>
                    <span>Queue Length</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl p-8 mb-12">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>配置线程池时，<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">线程池中的存活时间</code>（也称为<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">keep-alive time</code>）和<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">队列长度</code>是两个重要的参数。这些参数的设置取决于具体的应用需求、负载特性和系统资源。
            </p>
        </div>

        <!-- Visual Diagram -->
        <div class="mb-12">
            <div class="mermaid">
                graph TD
                    A[线程池配置] --> B[存活时间]
                    A --> C[队列长度]
                    B --> D[任务持续时间]
                    B --> E[系统负载]
                    B --> F[任务波动性]
                    C --> G[任务到达率]
                    C --> H[处理能力]
                    C --> I[系统资源]
                    C --> J[拒绝策略]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style B fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#f5576c,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Keep-Alive Time Section -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <div class="content-card rounded-2xl p-8">
                <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                    <i class="fas fa-hourglass-half mr-3 text-purple-600"></i>存活时间
                </h2>
                <p class="text-gray-700 mb-6 leading-relaxed">
                    <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">存活时间</code>是指线程池中闲置线程保持活跃的时间。在达到这个时间后，线程池会终止这些闲置的线程，以节省资源。
                </p>
                
                <div class="space-y-6">
                    <div class="list-item">
                        <h4 class="font-bold text-lg mb-2 text-gray-800">任务的持续时间</h4>
                        <p class="text-gray-600 leading-relaxed">
                            如果任务的处理时间很短且任务到达频率较低，较短的存活时间可以减少线程池的开销。如果任务的处理时间较长且任务到达频率较高，较长的存活时间可以避免频繁创建和销毁线程带来的性能开销。
                        </p>
                    </div>
                    
                    <div class="list-item">
                        <h4 class="font-bold text-lg mb-2 text-gray-800">系统负载和资源</h4>
                        <p class="text-gray-600 leading-relaxed">
                            在高负载环境中，较长的存活时间可以减少线程的创建和销毁开销，保持线程池中的线程活跃。在资源紧张的环境中，较短的存活时间可以减少资源占用，防止线程池中积累过多的闲置线程。
                        </p>
                    </div>
                    
                    <div class="list-item">
                        <h4 class="font-bold text-lg mb-2 text-gray-800">任务的波动性</h4>
                        <p class="text-gray-600 leading-relaxed">
                            如果任务到达的时间间隔不规律，适当增加存活时间可以防止线程频繁创建和销毁，提供更好的性能。如果任务到达非常规律，可以根据任务的规律性来调整存活时间。
                        </p>
                    </div>
                </div>
            </div>
            
            <div class="space-y-6">
                <div class="parameter-card">
                    <div class="relative z-10">
                        <i class="fas fa-lightbulb text-4xl mb-4 block"></i>
                        <h3 class="text-2xl font-bold mb-3">最佳实践</h3>
                        <p class="leading-relaxed">
                            通常将存活时间设置为 60 秒是一个良好的起点。这个值在大多数场景下能够平衡资源利用和性能开销。
                        </p>
                    </div>
                </div>
                
                <div class="code-block p-6">
                    <pre class="text-sm"><code>// 创建一个线程池，线程闲置60秒后会被终止
ExecutorService executor = new ThreadPoolExecutor(
    corePoolSize,
    maximumPoolSize,
    60L, // 存活时间
    TimeUnit.SECONDS,
    new LinkedBlockingQueue<>()
);</code></pre>
                </div>
            </div>
        </div>

        <!-- Queue Length Section -->
        <div class="content-card rounded-2xl p-8 mb-12">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-list-ol mr-3 text-pink-600"></i>队列长度
            </h2>
            <p class="text-gray-700 mb-8 leading-relaxed text-lg">
                <code class="bg-pink-100 text-pink-800 px-2 py-1 rounded">队列长度</code>决定了线程池的任务队列可以容纳多少个待处理任务。它对线程池的吞吐量、响应时间和资源使用都有影响。
            </p>
            
            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <div class="flex items-start">
                        <i class="fas fa-chart-line text-2xl text-purple-600 mr-4 mt-1"></i>
                        <div>
                            <h4 class="font-bold text-lg mb-2 text-gray-800">任务的到达率</h4>
                            <p class="text-gray-600 leading-relaxed">
                                如果任务到达率较高，较长的队列可以减少任务被拒绝的概率，但需要适当设置，以防队列过长导致内存消耗过大。如果任务到达率较低，较短的队列可能更适合，可以减少内存占用。
                            </p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-purple-50 p-6 rounded-xl">
                    <div class="flex items-start">
                        <i class="fas fa-tachometer-alt text-2xl text-blue-600 mr-4 mt-1"></i>
                        <div>
                            <h4 class="font-bold text-lg mb-2 text-gray-800">任务处理能力</h4>
                            <p class="text-gray-600 leading-relaxed">
                                如果每个任务处理的时间较长，可以使用较长的队列来平衡任务的处理能力和线程池的线程数量。如果任务处理时间很短，较短的队列可能更适合，减少任务排队的时间。
                            </p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-gradient-to-br from-green-50 to-blue-50 p-6 rounded-xl">
                    <div class="flex items-start">
                        <i class="fas fa-memory text-2xl text-green-600 mr-4 mt-1"></i>
                        <div>